package com.biostime.dingdingbox.modules.db.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.dingdingbox.common.config.Global;
import com.biostime.dingdingbox.common.persistence.Page;
import com.biostime.dingdingbox.common.utils.StringUtils;
import com.biostime.dingdingbox.common.web.BaseController;
import com.biostime.dingdingbox.modules.db.entity.Box;
import com.biostime.dingdingbox.modules.db.entity.BoxCode;
import com.biostime.dingdingbox.modules.db.service.BoxCodeService;
import com.biostime.dingdingbox.modules.db.service.BoxService;

/**
 * 玩具盒实体表Controller
 * @author chenweicheng
 * @version 2016-06-28
 */
@Controller
@RequestMapping(value = "${adminPath}/db/boxCode")
public class BoxCodeController extends BaseController {

	@Autowired
	private BoxCodeService boxCodeService;
	
	@Autowired
	private BoxService boxService;
	
	@ModelAttribute
	public BoxCode get(@RequestParam(required=false) String id) {
		BoxCode entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = boxCodeService.get(id);
		}
		if (entity == null){
			entity = new BoxCode();
		}
		return entity;
	}

	@RequiresPermissions("db:boxCode:view")
	@RequestMapping(value = {"list", ""})
	public String list(BoxCode boxCode, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BoxCode> page = boxCodeService.findPage(new Page<BoxCode>(request, response), boxCode); 
		model.addAttribute("page", page);
		return "modules/db/boxCodeList";
	}

	@RequiresPermissions("db:boxCode:view")
	@RequestMapping(value = "form")
	public String form(BoxCode boxCode, Model model) {
		List<Box>  boxList = boxService.findList(new Box());
		model.addAttribute("boxCode", boxCode);
		model.addAttribute("boxList", boxList);
		return "modules/db/boxCodeForm";
	}

	@RequiresPermissions("db:boxCode:edit")
	@RequestMapping(value = "save")
	public String save(BoxCode boxCode, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, boxCode)){
			return form(boxCode, model);
		}
		boxCodeService.save(boxCode);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/db/boxCode/?repage";
	}
	
	@RequiresPermissions("db:boxCode:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, BoxCode boxCode, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				boxCodeService.delete(new BoxCode(id));
			}
		}else{
			boxCodeService.delete(boxCode);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/db/boxCode/?repage";
	}

}